﻿///***************************************************************
/// 시  스  템 : Updms.Crd
/// 단위시스템 : CRD
/// 계      층 : Presentation Layer
/// 프로그램명 : Crd2015r
/// 파  일  명 : Crd2015r.aspx.cs
/// 설      명 : 연간카드사용내역
///***************************************************************
/// 수정ID 일자       이름     내용
///---------------------------------------------------------------
/// CH001  2008-09-26 송시명   최초 작성
///***************************************************************
using System;
using System.Data;
using System.Collections;
using System.Web.UI;
using System.Web.UI.WebControls;

using SMFramework.Data;
using SMFramework.Utility;
using SMFramework.Web.UI.WebControls.SMGridView;

using Base.Dac;
using Updms.Crd.Exe;


namespace Updms.Crd
{
	public partial class Crd2015r : Updms.Common.BaseClass.PageBase
	{
		#region 전역영역

		//프로그램상수
		public static readonly string PID = "Crd2015r";
		public static readonly string gs_path_info = "/updms/Crd/" + PID + ".aspx";

		Global global = new Global();
		CommCode.Crd crd = new CommCode.Crd(LANG_SET);

		//검색
		protected string kw1 = "";	//카드코드
		protected string kw2 = "";	//기준년도
		protected string kw3 = "";	//결제구분(일시불/현금서비스)
		protected string kw4 = "";	//계정
		protected string kw5 = "";	//주요아이템

		//메뉴 선택값
		protected string gs_sm = "";

		// 페이지모드
		private string gs_page_mode = "";

		#endregion

		#region Page_Load, OnPageLoad

		protected void Page_Load(object sender, EventArgs e)
		{
			SetInit();
			if (!Page.IsPostBack)
			{
				// 최초 화면 로딩시 처리해야 할 부분
				OnPageLoad();
				SetEnabledYearBtn();
			}
		}

		// OnPageLoad
		private void OnPageLoad()
		{
			SetLiterals(global.GetLabelInfoByPgmId(PID, LANG_SET));
			SetCardInfo();
			SetBaseYear(ddl_search_card_cd.SelectedValue);
			SetBizDiv();
			SetAccount();
			SetImportantItem();
			SetSearchParameter();
		}

		#endregion

		#region InitializeComponent

		private void InitializeComponent()
		{
			this.Load += new System.EventHandler(this.Page_Load);
			this.PreRender += new System.EventHandler(this.Page_PreRender);
		}

		#endregion	

		#region SetInit, SetSearchParameter, ShowKeywordLabel

		//초기값설정
		private void SetInit()
		{
			lbl_search_flag.Text = "INIT";

			gs_sm = (Request.QueryString["sm"] == null || Request.QueryString["sm"].Trim().Length != 7)
					? global.GetDirDepth(PID) : Request.QueryString["sm"].ToString();
		}

		//검색파라미터
		private void SetSearchParameter()
		{
			switch (lbl_search_flag.Text.Trim().ToUpper())
			{
				case "INIT":
					if (String.IsNullOrEmpty(Request.QueryString["kw1"]))	// url에 값이 없으면
					{
						kw1 = ddl_search_card_cd.SelectedValue;
					}
					else
					{
						kw1 = Server.UrlDecode(Request.QueryString["kw1"]);
						ddl_search_card_cd.SelectedValue = kw1;
					}

					if (String.IsNullOrEmpty(Request.QueryString["kw2"]))	// url에 값이 없으면
					{
						kw2 = ddl_search_base_year.SelectedValue;
					}
					else
					{
						kw2 = Server.UrlDecode(Request.QueryString["kw2"]);
						ddl_search_base_year.SelectedValue = kw2;
					}

					if (String.IsNullOrEmpty(Request.QueryString["kw3"]))	// url에 값이 없으면
					{
						kw3 = rbl_search_biz_div.SelectedValue;
					}
					else
					{
						kw3 = Server.UrlDecode(Request.QueryString["kw3"]);
						rbl_search_biz_div.SelectedValue = kw3;
					}

					if (String.IsNullOrEmpty(Request.QueryString["kw4"]))	// url에 값이 없으면
					{
						kw4 = ddl_search_account.SelectedValue;
					}
					else
					{
						kw4 = Server.UrlDecode(Request.QueryString["kw4"]);
						ddl_search_account.SelectedValue = kw4;
					}

					if (String.IsNullOrEmpty(Request.QueryString["kw5"]))	// url에 값이 없으면
					{
						kw5 = ddl_search_important_item.SelectedItem.Text;
					}
					else
					{
						kw5 = Server.UrlDecode(Request.QueryString["kw5"]);
						ddl_search_important_item.SelectedItem.Text = kw5;
					}					
					break;
				case "SEARCH":
					kw1 = ddl_search_card_cd.SelectedValue;
					kw2 = ddl_search_base_year.SelectedValue;
					kw3 = rbl_search_biz_div.SelectedValue;
					kw4 = ddl_search_account.SelectedValue;
					kw5 = ddl_search_important_item.SelectedItem.Text;
					break;
			}
		}


		#endregion

		#region 마스터 Method (SetCardInfo, SetBaseYear, SetBizDiv, SetAccount, SetImportantItem)

		//카드정보에서 카드정보를 가져와서 ddl_search_card_cd에 채운다.
		private void SetCardInfo()
		{
			DataSet lo_ds = new DataSet();
			Crd1010m_NT _Crd1010m_NT = new Crd1010m_NT();

			lo_ds.Merge(_Crd1010m_NT.GetCardInfoList("A"));

			ddl_search_card_cd.Items.Clear();
			ddl_search_card_cd.DataSource = lo_ds;
			ddl_search_card_cd.DataMember = "UPDMS_CRD_INFO";
			ddl_search_card_cd.DataTextField = "Card_Nm_Yn";
			ddl_search_card_cd.DataValueField = "Card_Cd";
			ddl_search_card_cd.DataBind();
		}

		//기준년도 설정
		private void SetBaseYear(string ls_card_cd)
		{
			DataSet lo_ds = new DataSet();
			DataPack lo_dp = new DataPack();
			ddl_search_base_year.Items.Clear();

			lo_dp.AddProperty("ls_card_cd", typeof(string), ls_card_cd);

			using (Crd1011m_NT _Crd1011m_NT = new Crd1011m_NT())
			{
				lo_ds.Merge(_Crd1011m_NT.GetMinNMaxBizDt(lo_dp));
			}
			
			if (DataSetUtil.GetFldInt(lo_ds, "UPDMS_CRD_PAYMNT_DT", "Min_Dt_Y") > 0)
			{
				int li_min_dt_y = DataSetUtil.GetFldInt(lo_ds, "UPDMS_CRD_PAYMNT_DT", "Min_Dt_Y");
				int li_max_dt_y = DataSetUtil.GetFldInt(lo_ds, "UPDMS_CRD_PAYMNT_DT", "Max_Dt_Y");

				for (int i = li_min_dt_y; i <= li_max_dt_y; i++)
				{
					string ls_year = i.ToString();
					ddl_search_base_year.Items.Add(new ListItem(ls_year, ls_year));
				}

				//기본값으로 현재년도 선택
				if (WebControlUtil.CheckDdlValue(ddl_search_base_year, DateUtil.GetYearEx(0, "yyyy")))
				{
					ddl_search_base_year.SelectedValue = DateUtil.GetYearEx(0, "yyyy"); // 기본값은 현재년도
				}
				else
				{
					ddl_search_base_year.SelectedValue = li_max_dt_y.ToString();
				}
			}
			else
			{
				ddl_search_base_year.Items.Add(new ListItem(DateUtil.GetYearEx(0, "yyyy"), DateUtil.GetYearEx(0, "yyyy")));
				CallMsgBoxInAjax("MSG_ERR_142");	//지정된 결제일이 없습니다.\n'카드결제일관리'에서 추가해 주세요
			}
		}

		//결제구분
		private void SetBizDiv()
		{
			crd.SetBizDiv(rbl_search_biz_div);
		}

		//계정
		private void SetAccount()
		{
			crd.SetAccount(ddl_search_account);

			ddl_search_account.Items.Insert(0, new ListItem(DDL_SELECT, ""));
		}

		//주요아이템
		private void SetImportantItem()
		{
			crd.SetImportantItem(ddl_search_important_item);

			ddl_search_important_item.Items.Insert(0, new ListItem(DDL_SELECT, ""));
		}

		//카드변경시
		protected void ddl_search_card_cd_SelectedIndexChanged(object sender, EventArgs e)
		{
			SetBaseYear(ddl_search_card_cd.SelectedValue);
			SetEnabledYearBtn();
		}

		#endregion

		#region 마스터 Method (SetPrevNextYear, SetThisYear, SetEnabledYearBtn)

		//전년, 차년버튼 누르면
		private void SetPrevNextYear(int pi_year_cnt)
		{
			string ls_year = ddl_search_base_year.SelectedValue;
			string ls_calc_year = Convert.ToString(Convert.ToInt32(ls_year) + pi_year_cnt);

			if (WebControlUtil.CheckDdlValue(ddl_search_base_year, ls_calc_year))
			{
				ddl_search_base_year.SelectedValue = ls_calc_year;
			}

			//버튼처리
			SetEnabledYearBtn();
		}

		//당년설정
		private void SetThisYear()
		{
			string ls_this_year = DateUtil.GetNow("yyyy");
			ddl_search_base_year.SelectedValue = ls_this_year;
			SetEnabledYearBtn();
		}

		//전년, 차년버튼 처리
		private void SetEnabledYearBtn()
		{
			ibtn_prev_year.Enabled = (ddl_search_base_year.SelectedIndex == 0) ? false : true;
			ibtn_next_year.Enabled = (ddl_search_base_year.SelectedIndex == ddl_search_base_year.Items.Count - 1) ? false : true;
		}

		#endregion

		#region CRUD (SelectProcess, SearchProcess)

		//검색
		private void SelectProcess(DataPack lo_dp)
		{
			DataSet lo_ds = new DataSet();			

			using (Crd2015r_NT _Crd2015r_NT = new Crd2015r_NT())
			{
				switch (rbl_search_biz_div.SelectedValue)
				{
					case "AB001":	//일시불
						lo_ds.Merge(_Crd2015r_NT.GetYearlyMassUsingList(lo_dp));
						break;
					case "AB002":	//현금서비스
						lo_ds.Merge(_Crd2015r_NT.GetYearlyMassUsingList(lo_dp));
						break;
					case "AB003":	//할부
						lo_ds.Merge(_Crd2015r_NT.GetYearlyInstallmentUsingList(lo_dp));
						break;
				}				

				if (!DataSetUtil.CheckRowCount(lo_ds, "UPDMS_CRD_REPORT"))
				{
					CallMsgBoxInAjax("MSG_ERR_111");	// 등록된 내용이 없습니다
				}

				GridView1.DataSource = lo_ds;
				GridView1.DataBind();
			}
		}	

		//검색
		private void SearchProcess()
		{
			gs_page_mode = "R";
			lbl_search_flag.Text = "SEARCH";
			SetSearchParameter();

			DataPack lo_dp = new DataPack();

			lo_dp.AddProperty("ls_card_cd", typeof(string), kw1);
			lo_dp.AddProperty("ls_base_year", typeof(string), kw2);
			lo_dp.AddProperty("ls_biz_div", typeof(string), kw3);
			lo_dp.AddProperty("ls_account", typeof(string), kw4);
			lo_dp.AddProperty("ls_important_item", typeof(string), kw5);
			lo_dp.AddProperty("ls_lang_set", typeof(string), LANG_SET);
			
			SelectProcess(lo_dp);
		}

		#endregion

		#region Page_PreRender, ChangePageMode

		// Page_PreRender
		private void Page_PreRender(object sender, System.EventArgs e)
		{
			if (!Page.IsPostBack)
			{
				using (AccessCheck access = new AccessCheck())
				{
					//접근권한 체크
					if (!access.CheckPgmAccessGrant(PID, USER_ID, ACCESS_LOG_INFO))
					{
						Response.Redirect(URL_NO_AUTHORITY_REDIRECT_BASE);	//사용자의 권한은 요청하신 페이지를 볼 수 없습니다
					}
				}
			}
			ChangePageMode();
		}

		// ChangePageMode
		private void ChangePageMode()
		{
			switch (gs_page_mode)
			{
				case "":		// 처음 페이지로드시
					table_list_set.Visible = false;
					break;
				case "R":	// 검색버튼 클릭시
					table_list_set.Visible = true;
					break;
			}
			SetGridViewHeader(global.GetColumnListByPgmId(PID, LANG_SET));
		}

		#endregion

		#region GridView이벤트

		//RowDataBound
		protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
		{
			//헤더의 rowspan되는 Cell을 삭제함
			if (e.Row.RowType == DataControlRowType.Header)
			{
				e.Row.Cells[0].Visible = false;
			}
			
			if (e.Row.RowType == DataControlRowType.DataRow)
			{
				//마우스올렸을 때
				e.Row.Attributes.Add("onMouseOver", GRID_ONMOUSEOVER_COLOR);
				e.Row.Attributes.Add("onMouseOut", GRID_ONMOUSEOUT_COLOR);

				//셀바탕색
				if (rbl_weekend_div.SelectedValue == "Y")
				{
					for (int i=0; i<e.Row.Cells.Count; i++)
					{
						if (e.Row.Cells[i].Text.Length > 37)
						{
							string ls_day_of_week = e.Row.Cells[i].Text.Substring(27, 3);
							string ls_holiday_yn = e.Row.Cells[i].Text.Substring(30, 1);

							//주말처리
							if (ls_day_of_week == "Sat")
							{
								e.Row.Cells[i].Attributes.Add("class", "bg_bluesky");
							}
							else if (ls_day_of_week == "Sun")
							{
								e.Row.Cells[i].Attributes.Add("class", "bg_pink");
							}

							//휴일처리
							if (ls_holiday_yn == "1")
							{
								e.Row.Cells[i].Attributes.Add("class", "bg_pink");
							}
						}
					}
				}
			} //if

			//멀티헤더설정
			SortedList lo_sl = new SortedList();
			SMGridView sGv = new SMGridView();

			lo_sl.Add("1", GetXmlMsg("STR_015") + ",1,2");
			lo_sl.Add("2", GetXmlMsg("STR_011") + ",3,1");
			lo_sl.Add("3", GetXmlMsg("STR_012") + ",3,1");
			lo_sl.Add("4", GetXmlMsg("STR_013") + ",3,1");
			lo_sl.Add("5", GetXmlMsg("STR_014") + ",3,1");
			sGv.SetMultiHeader(e, lo_sl, "multi_header");
		}

		#endregion

		#region 버튼이벤트

		//전년
		protected void ibtn_prev_year_Click(object sender, EventArgs e)
		{
			SetPrevNextYear(-1);
			SearchProcess();
		}

		//당년
		protected void ibtn_this_year_Click(object sender, EventArgs e)
		{
			SetThisYear();
			SearchProcess();
		}

		//차년
		protected void ibtn_next_year_Click(object sender, EventArgs e)
		{
			SetPrevNextYear(1);
			SearchProcess();
		}

		//검색
		protected void ibtn_search_Click(object sender, EventArgs e)
		{
			SetEnabledYearBtn();
			SearchProcess();
		}

		//엑셀
		protected void ibtn_excel_Click(object sender, EventArgs e)
		{
			SetSearchParameter();

			DataSet lo_ds = new DataSet();
			DataPack lo_dp = new DataPack();

			lo_dp.AddProperty("ls_card_cd", typeof(string), kw1);
			lo_dp.AddProperty("ls_base_year", typeof(string), kw2);
			lo_dp.AddProperty("ls_biz_div", typeof(string), kw3);
			lo_dp.AddProperty("ls_account", typeof(string), kw4);
			lo_dp.AddProperty("ls_important_item", typeof(string), kw5);
			lo_dp.AddProperty("ls_lang_set", typeof(string), LANG_SET);

			using (Crd2015r_NT _Crd2015r_NT = new Crd2015r_NT())
			{
				switch (rbl_search_biz_div.SelectedValue)
				{
					case "AB001":	//일시불
						lo_ds.Merge(_Crd2015r_NT.GetYearlyMassUsingList(lo_dp));
						break;
					case "AB002":	//현금서비스
						lo_ds.Merge(_Crd2015r_NT.GetYearlyMassUsingList(lo_dp));
						break;
					case "AB003":	//할부
						lo_ds.Merge(_Crd2015r_NT.GetYearlyInstallmentUsingList(lo_dp));
						break;
				}
			}
			string fileName = PID + "(" + global.GetMenuTitle(PID, LANG_SET) + ")";
			ExcelUtil.CreateExcelFileX(this.Page, fileName, lo_ds, SetExcelHeaderX(global.GetColumnListByPgmId(PID, LANG_SET)));
		}

		#endregion
		
	}
}
